package indi.zhifa.study2024.nbr.monitor.util;

import indi.zhifa.study2024.nbr.monitor.gen.monitor.sys.po.GenMonitorSysDbEntity;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.sys.service.IGenMonitorSysDbDbService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class DbEntityMemo {

    private final IGenMonitorSysDbDbService mMonitorSysDbDbService;
    List<GenMonitorSysDbEntity> mDbEntityList;
    @Value(value = "${spring.profiles.active}")
    private String mProfile;
    int mCurDbIndex;

    @PostConstruct
    public void init(){
        mDbEntityList = mMonitorSysDbDbService.getGenMonitorSysDbEntityListByProfile(mProfile);
        mCurDbIndex = 0;
    }

    synchronized public String genDbCode(){
        GenMonitorSysDbEntity genMonitorSysDbEntity = mDbEntityList.get(mCurDbIndex);
        mCurDbIndex++;
        if(mCurDbIndex >= mDbEntityList.size()){
            mCurDbIndex = 0;
        }
        return genMonitorSysDbEntity.getDbKey();
    }

}
